﻿using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.Reporting.WebForms;
using pbvs.Models.ViewModels;

namespace pbvs.Reports
{
    public class ReportValuation
    {
        protected ReportViewer Rv { get; set; }
        protected IList<ValuationReport> Valuations { get; set; }

        public ReportValuation(ReportViewer rv)
        {
            Rv = rv;
            Valuations = new List<ValuationReport>
                             {
                                 new ValuationReport
                                     {
                                        CreatedDate = DateTime.Today,
                                        FirstName = "John",
                                        LastName = "Client",
                                        Address = "123 Anywhere St.",
                                        City = "Salt Lake City",
                                        State = "UT",
                                        Zip = "84120",
                                        ParticipantFirstName = "James",
                                        ParticipantLastName = "Doe",
                                        SurvivorFirstName = "Jane",
                                        SurvivorLastName = "Doe",
                                        Gender = "M", 
                                        ValuationDate = DateTime.Today,
                                        Dob = new DateTime(1955, 2, 8),
                                        RetirementDate = new DateTime(2016, 4, 15),
                                        MonthlyBenefit = 2456.35m,
                                        AnnuityType = "Life Insurance",
                                        SurvivorDob = new DateTime(1960, 1, 11),
                                        SurvivorMonthlyBenefit = 2103.34m,
                                        PresentValueAtProjectedDate = 1000000m,
                                        MaritalPortionOfPresentValue = 123543m
                                    }
                             };
            GenerateReport();
        }

        public ReportValuation(ReportViewer rv, IList<ValuationReport> valuations)
        {
            Rv = rv;
            Valuations = valuations;
            GenerateReport();
        }

        public void GenerateReport()
        {
            Rv.LocalReport.DataSources.Add(new ReportDataSource("dsValuation", Valuations));
            Rv.LocalReport.Refresh();
            RenderPdf();
        }

        private void RenderPdf()
        {
            const string reportType = "PDF";
            string mimeType;
            string encoding;
            string[] streams;
            string extension = "PDF";
            Warning[] warnings;
            const string deviceInfo = "<DeviceInfo><OutputFormat>PDF</OutputFormat><PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight></DeviceInfo>";
            var renderedBytes = Rv.LocalReport.Render(reportType, deviceInfo, out mimeType, out encoding,
                                                      out extension, out streams, out warnings);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = mimeType;
            HttpContext.Current.Response.AddHeader("content-disposition",
                                                              String.Format("inline; filename={0}.pdf", Guid.NewGuid().ToString()));
            HttpContext.Current.Response.BinaryWrite(renderedBytes);
            HttpContext.Current.Response.End();
        }
    }
}